package com.kuaifawu.lwnlawyerclient.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kuaifawu.lwnlawyerclient.Model.LWNModel_myQuestion;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangminglei on 15/1/28.
 */
public class LWNAdapter_myQuestionList extends ArrayAdapter {
    private LayoutInflater mInflater;
    private List<LWNModel_myQuestion> mItems;
    private Context context_this;

    public LWNAdapter_myQuestionList(Context context, int resource, List<LWNModel_myQuestion> objects) {
        super(context, resource, objects);
        context_this = context;
        this.mInflater = LayoutInflater.from(context);
        this.mItems = objects;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public LWNModel_myQuestion getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LWNModel_myQuestion item = getItem(position);
        ArrayList<String> array_img = item.getImg_thumbnail();

        String[] string_location = {"北京", "上海", "天津", "重庆", "黑龙江省", "吉林省", "辽宁省", "河北省", "山西省", "内蒙古", "山东省",
                "安徽省", "江苏省", "浙江省", "江西省", "福建省", "河南省", "湖北省", "湖南省", "广东省", "广西", "海南省", "四川省", "贵州省", "云南省", "西藏", "陕西省", "甘肃省", "青海省", "宁夏", "新疆",
                "香港", "澳门", "台湾省"};

        int kind_content = Integer.parseInt(item.getQuestion_type());
        int is_solve = Integer.parseInt(item.getIs_solve());
        String location = string_location[Integer.parseInt(item.getQuestion_area())];

        View_content view_content = null;
        View_image view_image = null;

        if (convertView == null) {
            if (array_img == null) {
                convertView = mInflater.inflate(R.layout.item_myquestion, parent, false);
                view_content = new View_content();
                view_content.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                view_content.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                view_content.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                view_content.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                view_content.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                view_content.item_solve = (TextView) convertView.findViewById(R.id.item_wait_state);
                convertView.setTag(R.layout.item_myquestion, view_content);
            } else {
                convertView = mInflater.inflate(R.layout.item_myquestion_image, parent, false);
                view_image = new View_image();
                view_image.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                view_image.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                view_image.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                view_image.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                view_image.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                view_image.imageView_one = (ImageView) convertView.findViewById(R.id.item_wait_imageone);
                view_image.imageView_two = (ImageView) convertView.findViewById(R.id.item_wait_imagetwo);
                view_image.item_solve = (TextView) convertView.findViewById(R.id.item_wait_state);
                convertView.setTag(R.layout.item_myquestion_image, view_image);
            }
        } else {
            if (array_img == null) {
                view_content = (View_content) convertView.getTag(R.layout.item_myquestion);
                if (view_content == null) {
                    convertView = mInflater.inflate(R.layout.item_myquestion, parent, false);
                    view_content = new View_content();
                    view_content.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                    view_content.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                    view_content.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                    view_content.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                    view_content.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                    view_content.item_solve = (TextView) convertView.findViewById(R.id.item_wait_state);
                    convertView.setTag(R.layout.item_myquestion, view_content);
                }
            } else {
                view_image = (View_image) convertView.getTag(R.layout.item_myquestion_image);
                if (view_image == null) {
                    convertView = mInflater.inflate(R.layout.item_myquestion_image, parent, false);
                    view_image = new View_image();
                    view_image.item_name = (TextView) convertView.findViewById(R.id.item_wait_name);
                    view_image.item_lingyu = (TextView) convertView.findViewById(R.id.item_wait_lingyu);
                    view_image.item_leixing = (TextView) convertView.findViewById(R.id.item_wait_leixing);
                    view_image.item_time = (TextView) convertView.findViewById(R.id.item_wait_time);
                    view_image.item_content = (TextView) convertView.findViewById(R.id.item_wait_content);
                    view_image.imageView_one = (ImageView) convertView.findViewById(R.id.item_wait_imageone);
                    view_image.imageView_two = (ImageView) convertView.findViewById(R.id.item_wait_imagetwo);
                    view_image.item_solve = (TextView) convertView.findViewById(R.id.item_wait_state);
                    convertView.setTag(R.layout.item_myquestion_image, view_image);
                }
            }
        }

        //设置数据
        if (array_img != null) {
            if (array_img.size() == 2) {
                BitmapUtils bitmapUtils = new BitmapUtils(context_this);
                bitmapUtils.display(view_image.imageView_one, array_img.get(0));
                bitmapUtils.display(view_image.imageView_two, array_img.get(1));
                view_image.imageView_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        System.out.println("one");
                    }
                });

                view_image.imageView_two.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        System.out.println("two");
                    }
                });
            } else {
                BitmapUtils bitmapUtils = new BitmapUtils(context_this);
                bitmapUtils.display(view_image.imageView_one, array_img.get(0));
                view_image.imageView_one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        System.out.println("one");
                    }
                });
            }

            if (kind_content < 10) {
                view_image.item_leixing.setText("个人问题");
            } else {
                view_image.item_leixing.setText("公司问题");
            }
            view_image.item_name.setText(location + "网友");

            switch (kind_content) {
                case 0: {
                    view_image.item_lingyu.setText("婚姻家庭");
                }
                break;
                case 1: {
                    view_image.item_lingyu.setText("房屋买卖");
                }
                break;
                case 2: {
                    view_image.item_lingyu.setText("债务纠纷");
                }
                break;
                case 3: {
                    view_image.item_lingyu.setText("劳动争议");
                }
                break;
                case 4: {
                    view_image.item_lingyu.setText("交通肇事");
                }
                break;
                case 5: {
                    view_image.item_lingyu.setText("刑事案件");
                }
                break;
                case 6: {
                    view_image.item_lingyu.setText("其他类型");
                }
                break;
                case 11: {
                    view_image.item_lingyu.setText("公司注册");
                }
                break;
                case 12: {
                    view_image.item_lingyu.setText("投资融资");
                }
                break;
                case 13: {
                    view_image.item_lingyu.setText("股权期权");
                }
                break;
                case 14: {
                    view_image.item_lingyu.setText("合同事项");
                }
                break;
                case 15: {
                    view_image.item_lingyu.setText("知识产权");
                }
                break;
                case 16: {
                    view_image.item_lingyu.setText("财税事务");
                }
                break;
                case 17: {
                    view_image.item_lingyu.setText("劳动人事");
                }
                break;
                case 18: {
                    view_image.item_lingyu.setText("债务纠纷");
                }
                break;
                case 19: {
                    view_image.item_lingyu.setText("其他问题");
                }
                break;
                default:
                    break;
            }
            view_image.item_content.setText(item.getQuestion());
            view_image.item_time.setText(item.getTime());
            if (is_solve == 0) {
                view_image.item_solve.setText("待评价");
                view_image.item_solve.setTextColor(0xF16100);
            } else {
                view_image.item_solve.setText("已解决");
                view_image.item_solve.setTextColor(0x53A5E3);
            }
        } else {
            if (kind_content < 10) {
                view_content.item_leixing.setText("个人问题");
            } else {
                view_content.item_leixing.setText("公司问题");
            }
            view_content.item_name.setText(location + "网友");
            switch (kind_content) {
                case 0: {
                    view_content.item_lingyu.setText("婚姻家庭");
                }
                break;
                case 1: {
                    view_content.item_lingyu.setText("房屋买卖");
                }
                break;
                case 2: {
                    view_content.item_lingyu.setText("债务纠纷");
                }
                break;
                case 3: {
                    view_content.item_lingyu.setText("劳动争议");
                }
                break;
                case 4: {
                    view_content.item_lingyu.setText("交通肇事");
                }
                break;
                case 5: {
                    view_content.item_lingyu.setText("刑事案件");
                }
                break;
                case 6: {
                    view_content.item_lingyu.setText("其他类型");
                }
                break;
                case 11: {
                    view_content.item_lingyu.setText("公司注册");
                }
                break;
                case 12: {
                    view_content.item_lingyu.setText("投资融资");
                }
                break;
                case 13: {
                    view_content.item_lingyu.setText("股权期权");
                }
                break;
                case 14: {
                    view_content.item_lingyu.setText("合同事项");
                }
                break;
                case 15: {
                    view_content.item_lingyu.setText("知识产权");
                }
                break;
                case 16: {
                    view_content.item_lingyu.setText("财税事务");
                }
                break;
                case 17: {
                    view_content.item_lingyu.setText("劳动人事");
                }
                break;
                case 18: {
                    view_content.item_lingyu.setText("债务纠纷");
                }
                break;
                case 19: {
                    view_content.item_lingyu.setText("其他问题");
                }
                break;
                default:
                    break;
            }
            view_content.item_content.setText(item.getQuestion());
            if(is_solve == 0){
                view_content.item_solve.setText("待评价");
                view_content.item_solve.setTextColor(Color.rgb(241, 97, 0));
            }
            else{
                view_content.item_solve.setText("已解决");
                view_content.item_solve.setTextColor(Color.rgb(142, 189, 234));
            }
            view_content.item_time.setText(item.getTime());
        }
        return convertView;
    }

    class View_content {
        private TextView item_name;
        private TextView item_lingyu;
        private TextView item_content;
        private TextView item_time;
        private TextView item_leixing;
        private TextView item_solve;
    }

    class View_image {
        private TextView item_name;
        private TextView item_lingyu;
        private TextView item_content;
        private TextView item_time;
        private TextView item_leixing;
        private ImageView imageView_one;
        private ImageView imageView_two;
        private TextView item_solve;
    }
}
